package com.yanqu.road.server.manager.orewar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.CrossCenterServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


//盐场争霸活动
public class CrossOreWarActivityMgr extends TempMgr {

    public static final int CONDITION_TYPE_USER = 1;

    public static final int CONDITION_TYPE_UNION = 2;

    private static Logger logger = LogManager.getLogger(CrossOreWarActivityMgr.class.getName());

    private static Map<Integer,ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

    //activityId
    private static Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = new ConcurrentHashMap<>();

    //activityId type
    private static Map<Integer, Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoTypeMap = new ConcurrentHashMap<>();


    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean save() {
        return true;
    }

    @Override
    public boolean reloadData() {
        loadActivity();
        return true;
    }

    public static void loadActivity(){
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.OreWarActivity.getValue());
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        Map<Integer, ActivityInfo> tempActivityInfoMap =ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        Map<Integer, ActivityInfo> thisCrossActivityInfoMap = new ConcurrentHashMap<>();

        List<Integer> activityIdList = new ArrayList<>();
        long serverId = CrossBaseServer.getInstance().getServerId();
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            Map<Integer, UnionActivityGroup> groupMap  = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if(null != groupMap) {
                for (UnionActivityGroup group : groupMap.values()) {
                    if (group.getCrossServerId() == serverId) {
                        activityIdList.add(activityInfo.getActivityId());
                        thisCrossActivityInfoMap.put(activityInfo.getActivityId(),activityInfo);
                        break;
                    }
                }
            }
        }

        Map<Integer, List<ActivityConditionInfo>> tempActivityConditionInfoMap = new ConcurrentHashMap<>();
        Map<Integer, Map<Integer, List<ActivityConditionInfo>>> tempActivityConditionTypeInfoMap = new ConcurrentHashMap<>();
        ActivityBussiness.getActivityConditionInfoMap(tempActivityConditionTypeInfoMap, tempActivityConditionInfoMap, activityIdList);
        activityInfoMap = thisCrossActivityInfoMap;
        activityConditionInfoMap = tempActivityConditionInfoMap;
        activityConditionInfoTypeMap = tempActivityConditionTypeInfoMap;
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            loadNewActivity(activityInfo);
        }
        CrossOreWarMgr.removeActivity();
    }

    /**
     * 加载活动
     * @param
     * @return
     */
    private static void loadNewActivity(ActivityInfo activityInfo) {
        int activityId = activityInfo.getActivityId();
        int type = activityInfo.getType();
        int childType = activityInfo.getChildType();
        if (type == eActivityType.OreWarActivity.getValue() && childType == eActivityOreWarType.CrossOreWar.getChildType()) {
            CrossOreWarMgr.loadActivity(activityId);
        }
    }

    /**
     * 刷新活动配置
     * @param activityId    活动ID
     * @return
     */
    public static ActivityInfo reloadActivityInfo(int activityId) {
        ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
        if (activityInShowTime(activityInfo)) {
            activityInfoMap.put(activityId, activityInfo);
            return activityInfo;
        }
        return null;
    }

    public static ActivityInfo getOpenActivityInfo(int activityId){
        if(!activityInfoMap.containsKey(activityId)){
            return null;
        }
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if(activityInShowTime(activityInfo) && activityInfo.getActivityId() == activityId){
            return activityInfo;
        }
        return null;
    }

    public static List<ActivityInfo> getOpenActivityInfoList() {
        List<ActivityInfo> dataList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInShowTime(activityInfo)) {
                dataList.add(activityInfo);
            }
        }
        return dataList;
    }

    public static Map<Integer,ActivityInfo> getOpenActivityInfoMap() {
        Map<Integer,ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (activityInShowTime(activityInfo)) {
                dataMap.put(activityInfo.getActivityId(),activityInfo);
            }
        }
        return dataMap;
    }

    public static boolean activityInShowTime(ActivityInfo activityInfo){
        if(null == activityInfo){
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return nowTime >= activityInfo.getBeginShowTime() && nowTime < activityInfo.getEndShowTime();
    }

    public static boolean activityInTime(ActivityInfo activityInfo) {
        if(null == activityInfo){
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        return activityInfo.getBeginTime() <= now && activityInfo.getEndTime() > now;
    }

    public static ActivityInfo getInTimeActivityInfo(int activityId){
        if(!activityInfoMap.containsKey(activityId)){
            return null;
        }
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if(activityInTime(activityInfo) && activityInfo.getActivityId() == activityId){
            return activityInfo;
        }
        return null;
    }

    public static List<ActivityConditionInfo> getActivityConditionInfoList(int activityId){
        List<ActivityConditionInfo> activityConditionInfoList = activityConditionInfoMap.get(activityId);
        if(null != activityConditionInfoList){
            return new ArrayList<>(activityConditionInfoList);
        }
        return new ArrayList<>();
    }

    public static ActivityConditionInfo getActivityConditionInfoByRank(int activityId, int rank) {
        List<ActivityConditionInfo> dataList = getActivityConditionInfoList(activityId);
        if(null != dataList) {
            for (ActivityConditionInfo conditionInfo : dataList) {
                if (rank >= conditionInfo.getParamList().get(0).intValue() && rank <= conditionInfo.getParamList().get(1).intValue()) {
                    return conditionInfo;
                }
            }
        }
        return null;
    }

    public static ActivityConditionInfo getActivityConditionInfo(int activityId,int conditionId) {
        List<ActivityConditionInfo> activityConditionInfoList = getActivityConditionInfoList(activityId);
        for(ActivityConditionInfo activityConditionInfo : activityConditionInfoList){
            if(activityConditionInfo.getConditionId() == conditionId){
                return activityConditionInfo;
            }
        }
        return null;
    }

    public static int getMaxRankByType(int activityId, int type) {
        int maxRank = 0;
        List<ActivityConditionInfo> dataList = getActivityConditionInfoList(activityId);
        if(null != dataList) {
            for (ActivityConditionInfo conditionInfo : dataList) {
                if (conditionInfo.getParamList().get(1).intValue() > maxRank) {
                    maxRank = conditionInfo.getParamList().get(1).intValue();
                }
            }
        }
        return maxRank;
    }


    public static Map<Integer, List<ActivityConditionInfo>> getActivityConditionInfoMap(int activityId) {
        ActivityInfo inTimeActivityInfo = getOpenActivityInfo(activityId);
        if(null != inTimeActivityInfo) {
            return activityConditionInfoMap;
        }
        return null;
    }
}
